/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
 func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
    if root == nil {
        return root
    }
    if root.Val == q.Val || root.Val == p.Val {
        return root
    }
    leftNode := lowestCommonAncestor(root.Left, p, q)
    RightNode := lowestCommonAncestor(root.Right, p, q)
    if leftNode != nil && RightNode == nil{
        return leftNode
    } else if leftNode == nil && RightNode != nil{
        return RightNode
    } else if leftNode != nil && RightNode != nil{
        return root
    }
    return nil
}